package com.openbravo.pos.panels.link;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.jdesktop.swingx.JXTable;

import e.odbo.data.bean.IKeyed;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.db.table.I_Product;
import cn.ekuma.util.SwingUtil;
import com.openbravo.bean.Product;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;
import com.openbravo.pos.panels.QuickTimerFilter;
import com.openbravo.pos.panels.event.TimeCycleChangedEvent;
import com.openbravo.pos.panels.event.TimeCycleChangedListener;
import com.openbravo.pos.panels.model.ProductTableModel;

public class JSeachProductPanel extends AbstractProductSelectTablePanel<Product> implements  TimeCycleChangedListener{

	private QuickTimerFilter timerFilter;
	private JTextField inputCode;
	private JButton button_3;
	QBFParameters qbfParameters,productSeachQbfParameters;
	QBFParameter startDate,endDate,code,ref,name,origin,mnemonic;
	

	public JSeachProductPanel(I_DataLogicSales dlSales) {
		super(dlSales, new ProductTableModel());
		jXTable1.setFocusable(false);
		jXTable1.setRequestFocusEnabled(false);
		jXTable1.setAutoStartEditOnKeyStroke(false);
		jXTable1.setAutoResizeMode(JXTable.AUTO_RESIZE_OFF);
		qbfParameters=new QBFParameters();
		startDate=new QBFParameter(I_Product.CURDATE);
		endDate=new QBFParameter(I_Product.CURDATE);
		qbfParameters.and(startDate).and(endDate);
		
		code=new QBFParameter(I_Product.CODE);
		ref=new QBFParameter(I_Product.REFERENCE);
		name=new QBFParameter(I_Product.NAME);
		origin=new QBFParameter(I_Product.ORIGIN);
		mnemonic=new QBFParameter(I_Product.MNEMONIC);
		
		productSeachQbfParameters=new QBFParameters();
		productSeachQbfParameters.or(code).or(ref).or(name).or(origin).or(mnemonic);
	}

	@Override
	public void baseBeanChanged(IKeyed<String> key, Product obj) {
		
	}


	@Override
	public void baseBeanChanged(Product o) {
		
	}

	@Override
	public void timeCycleChanged(TimeCycleChangedEvent e) {
		startDate.setQbf((e.getStartDate()== null)? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_GREATEROREQUALS);
		startDate.setValue(e.getStartDate());
	    endDate.setQbf(( e.getEndDate() == null) ? QBFCompareEnum.COMP_NONE: QBFCompareEnum.COMP_LESS);
	    endDate.setValue(e.getEndDate());
		try {
			tableModel.resert(dlSales.query(Product.class, qbfParameters));
			SwingUtil.PreferredJXTableColumn(jXTable1);
		} catch (BasicException ex) {
				new MessageInf(ex).show(this);
		}
		
	}

	@Override
	public String getProductId(Product obj) {
		return obj.getID();
	}

	@Override
	public JPanel getToolBar() {
		JPanel toolbar=new JPanel();
		
        toolbar.setLayout(new javax.swing.BoxLayout(toolbar, javax.swing.BoxLayout.LINE_AXIS));
        timerFilter=new QuickTimerFilter();
        toolbar.add(timerFilter);
        timerFilter.addTimeCycleChangedListener(this);

        inputCode = new JTextField();
        
        inputCode.setPreferredSize(new Dimension(100, 21));
        toolbar.add(inputCode);
        
        inputCode.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				m_jBarcode1ActionPerformed(null);
			}
    	});
        
        button_3 = new JButton(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/apply.png")));
        button_3.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e) {
        		m_jBarcode1ActionPerformed(e);
        	}
        });
        toolbar.add(button_3);
        return toolbar;
	}

	protected void m_jBarcode1ActionPerformed(ActionEvent e) {
    	String sCode=inputCode.getText();
    	if(sCode.isEmpty()){
            return ;
         }
         try {
             if(sCode.matches("^[~～][A-Z0-9]+")){
                   java.util.List<Product> prods=dlSales.listProductByMnemonic(sCode.substring(1));
                   if(prods!=null&&prods.size()>0)
                	   tableModel.resert(prods);   
             }else{
            	code.equal(sCode);
         		ref.equal(sCode);
         		name.like(sCode);
         		origin.like(sCode);
         		mnemonic.like(sCode);
         		tableModel.resert(dlSales.query(Product.class, productSeachQbfParameters));	
             }
             SwingUtil.PreferredJXTableColumn(jXTable1);
         } catch (BasicException eData) {  
             new MessageInf(eData).show(this);           
         }
	}
	
}
